﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package GameMode {
    import Engine.Classes.*;
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Events.*;
    import flash.utils.*;
    import Display.*;
    import com.facebook.*;
    import com.facebook.events.*;
    import com.facebook.net.*;
    import com.facebook.data.*;
    import com.adobe.images.*;
    import com.facebook.data.photos.*;
    import Engine.*;
    import com.facebook.commands.users.*;
    import com.facebook.commands.photos.*;
    import com.facebook.data.auth.*;
    import flash.external.*;

    public class GMSnapShot extends GMDefault {

        private static const OUTLINE_WIDTH:int = 3;
        private static const OUTLINE_BORDER_WIDTH:int = 2;

        private static var m_soundLoaded:Boolean = false;

        private var m_image:Sprite;
        private var m_snapShotObject:Sprite;
        private var m_snapshotPos:Vector2;
        private var m_snapshotSize:Vector2;
        private var m_hasPermissions:Boolean = false;
        private var m_snapshot:ByteArray;
        private var m_album:AlbumData;
        private var m_fbook:Facebook;
        private var m_aid:String;
        private var m_photoDialog:MovieClip;
        private var m_cancelButton:Loader;
        private var m_message:String;

        public function GMSnapShot(){
            this.m_image = new Sprite();
            this.m_snapshotPos = new Vector2();
            this.m_snapshotSize = new Vector2();
            this.m_fbook = (GlobalEngine.socialNetwork as FacebookUtil).facebook;
            if (!m_soundLoaded){
                m_soundLoaded = true;
                SoundManager.addSound("camera", Global.getAssetURL("assets/sounds/actions/sfx_camera.mp3"), false);
            };
            var _local1:String = Global.getVisiting();
            super();
            setVisit(_local1);
            Global.ui.visible = false;
            this.getAlbums();
        }
        override public function enableMode():void{
            super.enableMode();
            var _local1:FacebookCall = (GlobalEngine.socialNetwork as FacebookUtil).facebook.post(new HasAppPermission(ExtendedPermissionValues.PHOTO_UPLOAD, (GlobalEngine.socialNetwork as FacebookUtil).facebook.uid));
            _local1.addEventListener(FacebookEvent.COMPLETE, this.onExtendedPermissionsCheck);
            StatsManager.count("take_photo", "start");
        }
        override public function disableMode():void{
            if (this.m_image.parent != null){
                Global.stage.removeChild(this.m_image);
                this.m_image = null;
            };
            Global.ui.visible = true;
            super.disableMode();
        }
        override protected function updateOverlay():void{
            if (this.m_snapShotObject != null){
                this.m_snapShotObject.graphics.clear();
                this.m_snapShotObject.graphics.lineStyle((2 * 2), 0xFF0000);
                this.m_snapShotObject.graphics.moveTo(this.m_snapshotPos.x, this.m_snapshotPos.y);
                this.m_snapShotObject.graphics.lineTo((this.m_snapshotPos.x + this.m_snapshotSize.x), this.m_snapshotPos.y);
                this.m_snapShotObject.graphics.lineTo((this.m_snapshotPos.x + this.m_snapshotSize.x), (this.m_snapshotPos.y + this.m_snapshotSize.y));
                this.m_snapShotObject.graphics.lineTo(this.m_snapshotPos.x, (this.m_snapshotPos.y + this.m_snapshotSize.y));
                this.m_snapShotObject.graphics.lineTo(this.m_snapshotPos.x, this.m_snapshotPos.y);
                this.m_snapShotObject.graphics.lineStyle(2, 0xFF0000);
                this.m_snapShotObject.graphics.moveTo(this.m_snapshotPos.x, this.m_snapshotPos.y);
                this.m_snapShotObject.graphics.lineTo((this.m_snapshotPos.x + this.m_snapshotSize.x), this.m_snapshotPos.y);
                this.m_snapShotObject.graphics.lineTo((this.m_snapshotPos.x + this.m_snapshotSize.x), (this.m_snapshotPos.y + this.m_snapshotSize.y));
                this.m_snapShotObject.graphics.lineTo(this.m_snapshotPos.x, (this.m_snapshotPos.y + this.m_snapshotSize.y));
                this.m_snapShotObject.graphics.lineTo(this.m_snapshotPos.x, this.m_snapshotPos.y);
            };
        }
        override public function onMouseMove(_arg1:MouseEvent):Boolean{
            var _local2:Point = getMouseStagePos();
            var _local3:Vector2 = new Vector2((_local2.x - (this.m_snapshotSize.x / 2)), (_local2.y - (this.m_snapshotSize.y / 2)));
            _local3.x = Utilities.clamp(_local3.x, 0, (Global.stage.stageWidth - this.m_snapshotSize.x));
            _local3.y = Utilities.clamp(_local3.y, 0, (Global.stage.stageHeight - this.m_snapshotSize.y));
            this.m_snapshotPos = _local3;
            this.updateOverlay();
            super.onMouseMove(_arg1);
            return (true);
        }
        override protected function getSelectableTypes():int{
            return (0);
        }
        override protected function handleClick(_arg1:MouseEvent):void{
            this.m_image.visible = false;
            if (((!((this.m_cancelButton == null))) && (!((this.m_cancelButton.parent == null))))){
                Global.stage.removeChild(this.m_cancelButton);
            };
            var _local2:BitmapData = new BitmapData(this.m_snapshotSize.x, this.m_snapshotSize.y, true, 0);
            var _local3:Matrix = new Matrix();
            _local3.translate(-(this.m_snapshotPos.x), -(this.m_snapshotPos.y));
            _local2.draw(Global.stage, _local3, null, null, null, true);
            var _local4:JPGEncoder = new JPGEncoder(85);
            var _local5:ByteArray = _local4.encode(_local2);
            this.m_snapshot = _local5;
            this.m_photoDialog = new TakePhotoPopup(_local2);
            this.m_photoDialog.addEventListener(TakePhotoEvent.ACCEPT, this.onDialogAccept);
            this.m_photoDialog.addEventListener(TakePhotoEvent.CANCEL, this.onDialogCancel);
            UI.displayPopup(this.m_photoDialog);
        }
        private function onDialogAccept(_arg1:TakePhotoEvent):void{
            this.m_message = _arg1.data;
            this.uploadPhoto();
            if (!this.m_hasPermissions){
            } else {
                this.onPermissionDialogClosed();
            };
        }
        private function onDialogCancel(_arg1:TakePhotoEvent):void{
            if (Global.isVisiting()){
                Global.world.addGameMode(new GMVisit(Global.getVisiting()), true);
            } else {
                Global.world.addGameMode(new GMDefault(), true);
            };
            Global.ui.visible = true;
        }
        private function onPermissionDialogClosed():void{
        }
        private function uploadPhoto():void{
            var _local1:String;
            if (Global.isVisiting()){
                _local1 = ("?ref=takePhoto&visitId=" + Global.getVisiting());
            } else {
                _local1 = "?ref=takePhoto";
            };
            var _local2:UploadPhoto = new UploadPhoto(this.m_snapshot, this.m_aid, this.m_message);
            _local2.addEventListener(FacebookEvent.COMPLETE, this.onPhotoUploadComplete, false, 0, true);
            this.m_fbook.post(_local2);
            if (Global.isVisiting()){
                Global.world.addGameMode(new GMVisit(Global.getVisiting()), true);
            } else {
                Global.world.addGameMode(new GMDefault(), true);
            };
            Global.ui.visible = true;
            StatsManager.count("take_photo", "upload");
            StatsManager.sendStats(true);
        }
        private function onPhotoUploadComplete(_arg1:FacebookEvent):void{
            if (_arg1.success){
            };
            StatsManager.count("take_photo", "upload_complete");
            StatsManager.sendStats(true);
        }
        private function getAlbums():void{
            var _local1:FacebookCall = this.m_fbook.post(new GetAlbums(this.m_fbook.uid));
            _local1.addEventListener(FacebookEvent.COMPLETE, this.onGetAlbums);
        }
        private function onGetAlbums(_arg1:FacebookEvent):void{
            var _local4:AlbumData;
            var _local6:FacebookCall;
            var _local2:Boolean;
            var _local3:GetAlbumsData = (_arg1.data as GetAlbumsData);
            var _local5:int;
            while (_local5 < _local3.albumCollection.length) {
                _local4 = (_local3.albumCollection.getItemAt(_local5) as AlbumData);
                if (_local4.name == "FarmVille Photos"){
                    this.m_aid = _local4.aid;
                    _local2 = true;
                };
                _local5++;
            };
            if (!_local2){
                _local6 = this.m_fbook.post(new CreateAlbum("FarmVille Photos"));
                _local6.addEventListener(FacebookEvent.COMPLETE, this.onCreateAlbum);
            };
        }
        private function onCreateAlbum(_arg1:FacebookEvent):void{
            this.getAlbums();
        }
        private function handleGetPhotosResponse(_arg1:FacebookEvent):void{
            var _local3:String;
            var _local4:Array;
            var _local2:GetPhotosData = (_arg1.data as GetPhotosData);
            if (_local2.photoCollection.length > 0){
                _local3 = _local2.photoCollection.getItemAt((_local2.photoCollection.length - 1))["src"];
                _local4 = [];
                _local4.push({
                    src:_local3,
                    href:"{SN_APP_URL}{creative:10}index.php"
                });
                GlobalEngine.socialNetwork.publishFeedStory("takePhoto", {images:_local4}, [], false);
            };
        }
        private function onExtendedPermissionsCheck(_arg1:FacebookEvent):void{
            if (((_arg1.success) && ((_arg1.data as BooleanResultData).value))){
                this.m_hasPermissions = true;
                this.createPhotoOutline();
            } else {
                this.m_hasPermissions = false;
                UI.displayMessage(ZLoc.t("Main", "Snapshot_dialog"), 1, this.onNewMessage);
            };
        }
        private function createPhotoOutline():void{
            this.m_snapShotObject = new Sprite();
            this.m_image = this.m_snapShotObject;
            this.updateOverlay();
            Global.stage.addChild(this.m_image);
            this.m_snapshotSize.x = 400;
            this.m_snapshotSize.y = 400;
            if (Global.stage.displayState == StageDisplayState.FULL_SCREEN){
                Global.stage.displayState = StageDisplayState.NORMAL;
            };
            this.m_cancelButton = LoadingManager.load(Global.getAssetURL("assets/dialogs/Cancel_Bt.swf"), this.onCancelButtonAdded, LoadingManager.PRIORITY_HIGH);
        }
        private function onPhotoCancel(_arg1:Event):void{
            Global.stage.removeChild(this.m_cancelButton);
            this.m_cancelButton.removeEventListener(MouseEvent.CLICK, this.onPhotoCancel);
            Global.world.removeGameMode(this);
            this.onDialogCancel(null);
        }
        private function onCancelButtonAdded(_arg1:Event):void{
            Global.stage.addChild(this.m_cancelButton);
            var _local2:Point = Global.ui.getBottomRight();
            this.m_cancelButton.x = ((_local2.x - this.m_cancelButton.width) - 10);
            this.m_cancelButton.y = ((_local2.y - this.m_cancelButton.height) - 10);
            this.m_cancelButton.addEventListener(MouseEvent.CLICK, this.onPhotoCancel);
        }
        private function onNewMessage(_arg1:GenericPopupEvent):void{
            if (!_arg1.button){
                Global.world.removeGameMode(this);
                this.onDialogCancel(null);
            } else {
                this.createPhotoOutline();
                ExternalInterface.addCallback("onPermissionDialogClosed", this.onPermissionDialogClosed);
                GlobalEngine.socialNetwork.showStreamPermissions();
                StatsManager.count("take_photo", "prompt_permission");
            };
        }

    }
}//package GameMode 
